package com.kejiguan.ui.util;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import com.kejiguan.ui.LoginActivity;
import com.kejiguan.ui.MainActivity;
import com.kejiguan.ui.R;
import com.kejiguan.ui.response.AlarmResponse;

import java.util.Random;

/**
 * Created by YYY on 16/7/9.
 */
public class PostNotifycation {

    public static PostNotifycation instance;

    public static PostNotifycation getInstance() {
        if (instance == null) {
            synchronized (PostNotifycation.class) {
                if (instance == null) {
                    instance = new PostNotifycation();
                }
            }
        }
        return instance;
    }

    private NotificationManager mNotificationManager;
    /**
     * notify id
     */
    private static int mNotifyID = 89;

    /**
     * @param context
     */
    public void showPostNotifycation(final Context context,AlarmResponse response){
        if (context == null) {
            return;
        }
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(context.getString(R.string.app_name))
                        .setContentText(response.getAlarmContent())
                        .setTicker(context.getString(R.string.app_name));
        boolean postResult = true;
        if (postResult) {
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(context, LoginActivity.class);
            resultIntent.putExtra("alarm_info",response);


            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            // Adds the back stack for the Intent (but not the Intent itself)
//            stackBuilder.addParentStack(HiRoadHomeActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                            0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
        }
        if (mNotificationManager == null) {
            mNotificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
        }
        mNotificationManager.cancel(mNotifyID);

//        mNotifyID = new Random().nextInt(10000);
        if (mNotificationManager != null) {
            // mId allows you to update the notification later on.
            mNotificationManager.notify(mNotifyID, mBuilder.build());
        }
    }

    /**
     * 消除提醒
     */
    public void clearPostNotifycation(Context context) {
        if (mNotificationManager == null && context != null) {
            mNotificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
        } else if (mNotificationManager != null) {
            mNotificationManager.cancel(mNotifyID);
        }
    }
}
